#include <iostream>
#include <vector>
#include <cassert>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "test.hpp"
using namespace std;

int main()
{
    int fd[2] = {0};
    int n = pipe(fd);
    assert(n != -1);

    pid_t id = fork();
    assert(id != -1);
    if(id == 0)
    {
        //读取
        close(fd[1]);
        //dup2(fd[0],0);
        while(true)
        {
            char str[64] = {0};
            int n = read(fd[0],str,sizeof(str)-1);
            if(n == 0) cout<<"不等待"<<endl;
            sleep(1);
            if(strcasecmp(str,"quit") == 0) break;
            cout<<str<<endl;
        }
        close(fd[0]);
        exit(0);
    }

    //父进程
    close(fd[0]);

    while(true)
    {
        char str[1024] = {0};
        scanf("%s",str);
        str[strlen(str)] = 0;
        write(fd[1],str,strlen(str));
        if(strcasecmp(str,"quit") == 0) break;
    }
    close(fd[1]);
    wait(NULL);
    return 0;
}
